import tomllib
import subprocess


def build_game():
    cmd = ['python', '-m', 'nuitka']

    with open("build.toml", "rb") as f:
        config = tomllib.load(f)

    if build := config.get('build'):
        if build.get('mingw64'):
            cmd.append('--mingw64')
        if build.get('onefile'):
            cmd.append('--onefile')
        if build.get('standalone'):
            cmd.append('--standalone')
        if build.get('disable-console'):
            cmd.append('--windows-console-mode=disable')
        if n := build.get('jobs'):
            cmd.append(f'--jobs={n}')
        if lto := build.get('lto'):
            cmd.append(f'--lto={lto}')

    if imports := config.get('imports'):
        if imports.get('follow-imports'):
            cmd.append(f'--follow-imports')

    if data := config.get('data'):
        if dirs := data.get('include-data-dir'):
            for dir in dirs:
                cmd.append(f'--include-data-dir={dir}={dir}')

    if output := config.get('output'):
        if output_dir := output.get('output-dir'):
            cmd.append(f'--output-dir={output_dir}')

        if output.get('remove-output'):
            cmd.append(f'--remove-output')

    if infos := config.get('infos'):
        if company_name := infos.get('company-name'):
            cmd.append(f'--company-name={company_name}')

        if product_name := infos.get('product-name'):
            cmd.append(f'--product-name={product_name}')

        if file_version := infos.get('file-version'):
            cmd.append(f'--file-version={file_version}')

        if product_version := infos.get('product-version'):
            cmd.append(f'--product-version={product_version}')

    if warn := config.get('warn'):
        if warn.get('warn-implicit-exceptions'):
            cmd.append(f'--warn-implicit-exceptions')

        if warn.get('warn-unusual-code'):
            cmd.append(f'--warn-unusual-code')

    cmd.append('main.py')

    subprocess.run(cmd)


if __name__ == "__main__":
    build_game()